package com.x.wx.controller;


import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jfinal.weixin.sdk.api.MediaApi;
import com.jfinal.weixin.sdk.api.MediaApi.MediaType;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.framework.model.BaseObject;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信素材
 */
@RestController
public class WxMaterialController extends BaseController {

	/**
	 * 查询素材列表
	 * @param obj
	 * @param mediaType
	 * @param appId
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMaterialList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getMaterialList(BaseObject obj, String mediaType, String appId) {
		if(StringUtils.isEmpty(mediaType)){
			return new ResultModel(ResultCode.RESULT_FAIL, "mediaType不能为空！");
		}
		if(StringUtils.isEmpty(appId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "appId不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", MediaApi.batchGetMaterial(appId, MediaType.valueOf(mediaType), (obj.getPageIndex() - 1) * obj.getPageSize(), obj.getPageSize()));
	}

	/**
	 * 查询素材信息
	 * @param mediaId
	 * @param appId
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMaterialById", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel getMaterialList(String mediaId, String appId) {
		if(StringUtils.isEmpty(mediaId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "mediaId不能为空！");
		}
		if(StringUtils.isEmpty(appId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "appId不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", MediaApi.getNews(appId, mediaId));
	}
}
